package com.example.demo.controller;

import com.example.demo.entity.*;
import com.example.demo.mapper.ProMemberMapper;
import com.example.demo.result.FunctionInfo;
import com.example.demo.result.ProjectInfo;
import com.example.demo.result.R;
import com.example.demo.result.Renum;
import com.example.demo.service.FunctionPointService;
import com.example.demo.service.ProjectService;
import com.example.demo.service.TaskService;
import com.example.demo.service.UserService;
import com.example.demo.utils.RUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/Project")
public class ProjectController {

    @Autowired
    ProjectService projectService;

    @Autowired
    FunctionPointService functionservice;

    @Autowired
    UserService userService;
    @Autowired
    TaskService taskService;


    @CrossOrigin
    @ResponseBody
    @RequestMapping("/addProject")
    public R addProject(String proName, Integer PMId, Integer proBelong, String proIntroduce){
        Project project = new Project();
        project.setProName(proName);
        project.setProManagerId(PMId);
        project.setProState(1);
        project.setProBelong(proBelong);
        project.setProIntroduce(proIntroduce);
        Date date = new Date(System.currentTimeMillis());
        project.setProDate(date);
        System.out.println(project);
        if(projectService.addProject(project)>0)
            return RUtils.success(project);
        return RUtils.error(Renum.ERROR);
    }

    @CrossOrigin
    @ResponseBody
    @RequestMapping("/stopProject")
    public R stopProject(int projectId,Integer PMId){
        Date date = new Date(System.currentTimeMillis());
        if (projectService.stopProject(projectId,0,date)>0)
            return RUtils.success(Renum.SUCCESS);
        return RUtils.error(Renum.ERROR);

    }


    //功能点列表
    @CrossOrigin
    @RequestMapping("/functionList")
    @ResponseBody
    public List<FunctionInfo> getFuncitonList(Integer functionProId){
        List<FunctionPoint> functionPoints = projectService.getFunctionPointByProjectId(functionProId);
        List<FunctionInfo> functionInfos = new ArrayList<>();

        for(int i=0;i<functionPoints.size();i++){
            FunctionInfo functionInfo = functionservice.getFunctionInfo(functionPoints.get(i));
            functionInfos.add(functionInfo);
        }

        return functionInfos;
    }

    @CrossOrigin
    @RequestMapping("/projectList")
    @ResponseBody
    public List<Project> getUserProjectListByUserId(int userId){
        return projectService.getUserProjectListByUserId(userId);
    }

    @CrossOrigin
    @RequestMapping("/getProMember")
    @ResponseBody
    public List<User> getProMemberList(Integer proId){
        System.out.println("222222222222");
        //System.out.println("userinfo"+projectService.getProMemberListByProId(proId));
        return projectService.getProMemberListByProId(proId);
    }

    @CrossOrigin
    @RequestMapping("/addProMember")
    @ResponseBody
    public R addProMember(@RequestParam List<Integer> proMemberIdList, Integer proId){
        System.out.println("11111111111");
        List<User> users = new ArrayList<>() ;
        List<User> proUserList = getProMemberList(proId);
        boolean flag;
        System.out.println("proUserList:"+proUserList);
        //项目成员列表为空，该项目目前还没邀请过成员，不用判读userId是否重复直接插入
        if(proUserList.isEmpty()){
            for (Integer proUserId: proMemberIdList){
                projectService.addProMember(proUserId,proId);
                users.add(userService.getById(proUserId));
            }
            System.out.println("users:"+users);
            return  RUtils.success(users);
        }

        for (Integer proUserId: proMemberIdList){
            flag = true;
            for (User user: proUserList){
                if(proUserId.equals(user.getUserId())){
                    flag = false;
                    break;
                }
            }
            if(flag){
                projectService.addProMember(proUserId,proId);
                users.add(userService.getById(proUserId));
            }
        }
        System.out.println("users:"+users);

        return  RUtils.success(users);
    }

    //测试添加项目成员
    @RequestMapping("/test")
    @ResponseBody
    public R test(){
        List<Integer> proMembers = new ArrayList<>();

        proMembers.add(11);
        proMembers.add(12);
        proMembers.add(5);
        proMembers.add(6);

        return addProMember(proMembers, 7);
    }

    @CrossOrigin
    @RequestMapping("/deleteProMember")
    @ResponseBody
    public R deleteProMember(Integer proId, Integer userId){
        if(projectService.deleteProMember(userId, proId)>0){
            return RUtils.success(Renum.SUCCESS);
        }
        return RUtils.error(Renum.ERROR);
    }

    @CrossOrigin
    @ResponseBody
    @RequestMapping("/getProMemberNum")
    public R getProMemberNum(Integer proId){
        return RUtils.success(projectService.proMemberNum(proId));
    }

    @CrossOrigin
    @ResponseBody
    @RequestMapping("/getProTasks")
    public R getProTasks(Integer proId){
        return RUtils.success(taskService.getProTaskList(proId));
    }

    @CrossOrigin
    @ResponseBody
    @RequestMapping("/getProTaskNum")
    public R getProTaskNum(Integer proId){
        return RUtils.success(taskService.proTaskNum(proId));
    }

    @CrossOrigin
    @ResponseBody
    @RequestMapping("/getProInfo")
    public R getProInfo(Integer proId){
        ProjectInfo projectInfo = new ProjectInfo();
        Project project = projectService.getInfo(proId);

        projectInfo.setProName(project.getProName());
        projectInfo.setPMId(project.getProManagerId());
        projectInfo.setMemberNum((int) projectService.proMemberNum(proId));
        projectInfo.setDocumentNum(0);
        projectInfo.setStorageNum(0);
        projectInfo.setTaskNum((int) taskService.proTaskNum(proId));
        projectInfo.setUsers(getProMemberList(proId));
        projectInfo.setTasks(taskService.getProTaskList(proId));

        return RUtils.success(projectInfo);
    }

}
